package com.feng.test.json;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

enum LineType {
    Arrow("startArrow"),
    None("none");

    private String value;

    LineType(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

class FastJsonTest {

    public static void test() {
        String json = "{\"title\":\"课件标题\",\"ppt\":{\"props\":{\"style\":{},\"animation\":{}},\"teachingLinks\":[{\"id\":\"\",\"title\":\"标题\",\"description\":\"\",\"props\":{},\"slides\":[{\"id\":\"\",\"props\":{\"background\":{\"image\":\"\",\"color\":\"\"}},\"exerciseId\":\"\",\"blocks\":[{\"id\":\"block-uuid\",\"type\":\"\",\"props\":{\"position\":{\"left\":0,\"top\":0},\"size\":{\"width\":0,\"height\":0},\"border\":{\"style\":\"\",\"width\":0,\"color\":\"\"},\"opacity\":\"\",\"rotation\":\"\",\"animation\":{\"effect\":\"\",\"duration\":\"\",\"delay\":\"\",\"index\":-1},\"zIndex\":\"\"}}]}]}],\"exercises\":[{\"id\":\"exercise-uuid\",\"title\":\"\",\"slides\":{\"slideId\":[{\"blockId\":\"\",\"content\":\"\",\"questionType\":\"\",\"options\":\"\",\"answer\":\"\"}]}}]}}";
        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONObject pptJsonObject = jsonObject.getJSONObject("ppt");
        JSONArray teachingLinksJsonArray = pptJsonObject.getJSONArray("teachingLinks");
        if (teachingLinksJsonArray != null && teachingLinksJsonArray.size() > 0) {
            for (int i = 0; i < teachingLinksJsonArray.size(); i++) {
                JSONObject teachingLinkJsonObject = teachingLinksJsonArray.getJSONObject(i);
                JSONArray slidesJsonArray = teachingLinkJsonObject.getJSONArray("slides");
                if (slidesJsonArray != null && slidesJsonArray.size() > 0) {
                    for (int j = 0; j < slidesJsonArray.size(); j++) {
                        JSONObject slidesJsonObject = slidesJsonArray.getJSONObject(j);
                        JSONArray blocksJsonArray = slidesJsonObject.getJSONArray("blocks");
                        if (blocksJsonArray != null && blocksJsonArray.size() > 0) {
                            for (int k = 0; k < blocksJsonArray.size(); k++) {
                                JSONObject blocksJsonObject = blocksJsonArray.getJSONObject(k);
                                System.err.println(blocksJsonObject.getString("ids"));
                            }
                        }
                    }
                }
            }


        }
    }

    public static void main(String[] args) {
        ResponseUtils responseUtils = new ResponseUtils(0, "name");
        System.out.println(responseUtils.toString());
        Person p = new Person(0, "name");
        System.out.println(JSON.toJSONString(p));
        String json = JSONObject
            .toJSONString(p, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
        System.out.println(json);

        System.out.println("----------------");
        p = new Person();
        p.setAge(10);
        p.setName("test");
        p.setLineType(LineType.Arrow);
        System.out.println("Json: " + JSON.toJSONString(p));
        System.out.println(p.toString());

    }
}

class ResponseUtils<T> {

    private T date;
    private String name;

    public ResponseUtils(T date, String name) {
        this.date = date;
        this.name = name;
    }

    @Override
    public String toString() {
        return "ResponseUtils{" +
            "date=" + date +
            ", name='" + name + '\'' +
            '}';
    }
}

class Person {

    private int age;
    private String name;
    private LineType lineType;

    public Person() {
    }

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
            "age=" + age +
            ", name='" + name + '\'' +
            ", lineType=" + lineType +
            '}';
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LineType getLineType() {
        return lineType;
    }

    public void setLineType(LineType lineType) {
        this.lineType = lineType;
    }
}

class TestJSONList {

    public static void main(String[] args) {
/*        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        String json = JSON.toJSONString(list);
        System.out.println(json);*/

/*        Person p = new Person();
        p.setAge(1);
        p.setName("ty");
        System.out.println(JSON.toJSONString(p));*/

        Person person = JSON.parseObject("{\"age\":1,\"name\":\"ty\"}", Person.class);
        System.out.println(person);
    }

}
